/*
 * Created by SharpDevelop.
 * User: Michael
 * Date: 17/09/2007
 * Time: 4:50 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

namespace SudokuSolver
{
	/// <summary>
	/// Lone Numbers is an agressive method which finds cells with only one candidate in their list.
	/// Cells with only one candidate are assigned that value.
	/// </summary>
	public class LoneNumbers : Technique
	{
		
		private int LastValue(Cell theCell)
		{
			for(int i = 1; i <= 9; i++)
			{
				if(theCell.IsPossible(i))
					return i;
			}
			
			return -1;
		}
		
		public override bool Run(SudokuGame theGame)
		{
			//Console.WriteLine("Starting LoneNumbers...");
			bool changed = false;
			//for each cell...
			foreach(Group row in theGame.Rows)
			{
				foreach(Cell cell in row.Cells)
				{
					if((cell.CountPossible() == 1) && (!cell.Found))
					{
						//work out what value is to go in here
						cell.Value = LastValue(cell);
						//something changed!
						//Console.WriteLine("Lone Numbers found that " + Cell.RowNumber + "," + cell.Column + " contains " + cell.Value);
						changed = true;
					}
				}
			}
			
			return changed;
		}
		
		public override string ToString()
		{
			return base.ToString() + ": Lone Numbers";
		}
	}
}
